//页面加载完成
$(function () {

    //抽取变量
    var feesearch_datagrid = $("#feesearch_datagrid"); //表格数据
    var feesearch_tabs = $("#feesearch_tabs"); //工具条
    var feesearch_dialog = $("#feesearch_dialog");//弹框
    var feesearch_form = $("#feesearch_form"); //表单信息

    //渲染查看表单中的数据列表
    $("#product_datagrid").datagrid({
        columns:[[
            {field:'name',title:'险种',width:100},
            {field:'annuaflee',title:'价格',width:100}
        ]]
    });
    //初始产品信息界面
    feesearch_datagrid.datagrid({
        fit: true,
        fitColumns: true,
        striped: true,
        url: '/issuepolicy/list',
        toolbar: "#feesearch_tabs",
        rownumbers: true,
        pagination: true,
        singleSelect: true,
        align:'center',
        columns: [[

            {field: 'serialNumber', title: '保单号', width: 80},
            {field: 'customer', title: '投保人', width: 80,
                formatter:function (value,row,index) {
                    return value ? value.name : '';
                }
            },
            {field: 'tel', title: '电话', width: 150,
                formatter:function (value,row,index) {
                    return row.customer? row.customer.tel : '';
            }},
            {field: 'addresss', title: '地址', width:100,
                formatter:function (value,row,index) {
                    return row.customer?row.customer.address:'';
                }},
            {field: 'idNo', title: '身份证', width: 150,
                formatter:function (value,row,index) {
                    return row.customer?row.customer.idNo:'';
                }
            },
            {field: 'sn', title: '核保单号', width: 150,
                formatter:function (value,row,index) {
                    return row.policy ? row.policy.sn : '';
                }
            },
            {field: 'totalAmount', title: '保费金额', width: 100},
            {field: 'state', title: '缴费状态', width: 100,
                formatter:function (value,row,index) {
                    if(value==0){
                        return '<span style="color: red">缴费失败</span>'
                    }
                    if(value==1){
                        return '<span style="color: green">缴费成功</span>'
                    }
                }
            },
            {field: 'paymentWay', title: '缴费方式', width: 100,
                formatter:function (value,row,index) {
                    if(value==0){
                        return '<span>刷卡支付</span>'
                    }
                    if(value==1){
                        return '<span>网上支付</span>'
                    }
                    if(value==2){
                        return '<span>现金支付</span>'
                    }
                }
            },
            {field: 'beginDate', title: '起保日期', width: 120},
            {field: 'endDate', title: '止保日期', width: 120},
            {field: 'safetymechanism', title: '保险机构', width: 100,
                formatter:function (value,row,index) {
                    return value?value.name:'';
                }
            },
            {field: 'inputUser', title: '录入人', width: 100,
                formatter:function (value,row,index) {
                    return row.inputUser? row.inputUser.userName : '';
                }
            }
        ]]
    });

    //将方法绑定到对象中去
    var cmdObj = {
        //刷新功能
        reloadIssue:function () {
            feesearch_datagrid.datagrid("reload");
        },
        //删除功能
        removeIssue:function () {
            //获取选中行
            var row = feesearch_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", "老铁请选择要删除的数据", "error");
                return;
            }
            $.messager.confirm("温馨提示", "你确定要删除吗?", function (yes) {
                if (yes) {
                    //发送ajax请求
                    $.get("/feesearch/remove?id=" + row.id, function (data) {

                        if (data.success) {
                            $.messager.alert("温馨提示", data.message, "info", function () {
                                cmdObj.reloadIssue();
                            })
                        }else{
                            $.messager.alert("温馨提示",data.message,"error");
                        }
                    });
                }
            });
        },
        //取消按钮功能
        cancelIssue:function () {

            feesearch_dialog.dialog("close");
        },
        //查看信息功能
        lookMsg:function () {
            //获取选中行
            var row = feesearch_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", "老铁请选择要查看的数据", "error");
                return;
            }
            feesearch_dialog.dialog("setTitle","信息列表");
            feesearch_dialog.dialog("open");
            feesearch_form.form("clear");

            //数据回显
            if (row.customer) {
                row["customer.name"] = row.customer.name;
                row["customer.idNo"] = row.customer.idNo;
                row["customer.tel"] = row.customer.tel;
            }
            if(row.policy){
                row["policy.sn"] = row.policy.sn;
            }
            if (row.safetymechanism) {
                row["safetymechanism.name"] = row.safetymechanism.name;
            }
            if(row.inputUser){
                row["inputUser.userName"] = row.inputUser.userName;
            }

            if(row.policy){
                row["row.sn"] = row.policy.sn;
            }
            $.post("/issuepolicy/selectProductBySn",{sn:row.policy.sn},function (data) {
                $("#product_datagrid").datagrid("loadData",data);
            });

            feesearch_form.form("load",row);
        },
        //查询功能
        searchIssue:function () {
            //获取高级查询条件
            var keyword = $("#keyword").val();
            var serialNumber = $("#serialNumber").val();
            var beginDate = $("#beginDate").val();
            var endDate = $("#endDate").val();

            //再次加载数据
            feesearch_datagrid.datagrid("load",{keyword:keyword,serialNumber:serialNumber,beginDate:beginDate,endDate:endDate});
        }
    };
   //初始化对话框
    feesearch_dialog.dialog({
        width: 310,
        height: 500,
        title:'新增信息',
        closed:true,
        buttons: "#issue_buttons"
    });

   //使用js来绑定事件  使用了属性过滤器来完成该操作
    $("a[data-cmd]").click(function(){
        var cmd = $(this).data("cmd");
        //调用方法
        cmdObj[cmd]();
    });


});
